/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rlpw.swing.table;

/**
 *
 * @author weinpau
 */
import java.awt.Color;
import java.awt.Component;

import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.table.TableCellRenderer;

public class BooleanRenderer extends JCheckBox implements TableCellRenderer {

    public BooleanRenderer() {
        setHorizontalAlignment(SwingConstants.CENTER);
    }

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
            int row, int column) {

        Boolean b = (Boolean) value;
        if (b != null) {
            setSelected(b);
        }
        setOpaque(true);

        if (isSelected) {
            setForeground(table.getSelectionForeground());
            setBackground(table.getSelectionBackground());
        } else {

            Color aColor = UIManager.getColor("Table.alternateRowColor");

            if (row % 2 == 1 || aColor == null) {
                setBackground(aColor);
            } else {
                setOpaque(false);
                setBackground(table.getBackground());
            }

        }
        return this;
    }
}
